home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Format 1994 October
/
Macformat17.cdr
/
Shareware City
/
Developers
/
shutdown-fx-201-c
/
sfx ƒ
/
sfx control app ƒ
/
sfx code ƒ
/
sfx install.c
< prev
next >
Wrap
Text File
|
1994-07-11
|
5KB
|
178 lines
/**********************************************************************\
File: sfx install.c
Purpose: This module handles installing shutdown procs and installing
and removing fades from the system heap.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program in a file named "GNU General Public License".
If not, write to the Free Software Foundation, 675 Mass Ave,
Cambridge, MA 02139, USA.
\**********************************************************************/
#include "Shutdown.h"
#include "sfx install.h"
#include "sfx gestalt.h"
#include "file interface.h"
#include "util.h"
enum ErrorTypes InstallTheShutdownProc(short index, Str255 theName, Boolean onShutdown,
Boolean onRestart, Boolean isEarly, Boolean installFade, Boolean installProcs)
/* assume: there are no shutdown procs currently installed */
/* assume: sfx Gestalt function is properly installed */
{
OSErr theResError;
FSSpec fs;
short oldRefNum, newRefNum;
Boolean alreadyOpen;
Handle theProcHandle;
ProcPtr theFade, theShutdownProc, theRestartProc;
unsigned long theSize;
unsigned long dummy;
enum ErrorTypes resultCode;
if (installFade)
{
MyMakeFSSpec(gModuleVRefNum, gModuleDirID, theName, &fs);
theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
if (theResError!=noErr)
return kCantOpenModule;
theProcHandle=Get1Resource('PROC', 0);
if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
return kModuleDamaged;
if (*theProcHandle==0L)
LoadResource(theProcHandle);
if (*theProcHandle==0L)
return kModuleDamaged;
theProcHandle=StripAddress(theProcHandle);
*theProcHandle=StripAddress(*theProcHandle);
HLock(theProcHandle);
theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
if (theFade==0L)
{
ReleaseResource(theProcHandle);
return kNoMemoryInSystemHeap;
}
theFade=StripAddress(theFade);
Mymemcpy(theFade, *theProcHandle, theSize);
ReleaseResource(theProcHandle);
theProcHandle=0L;
CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
}
else theFade=0L;
if (installProcs)
{
theProcHandle=Get1Resource('PROC', 10); /* get shutdown proc from sfx application */
if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
return kCantGetProc10Resource;
if (*theProcHandle==0L)
LoadResource(theProcHandle);
if (*theProcHandle==0L)
{
DisposePtr(theFade);
return kCantGetProc10Resource;
}
theProcHandle=StripAddress(theProcHandle);
*theProcHandle=StripAddress(*theProcHandle);
theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
theRestartProc=NewPtrSys(theSize);
theShutdownProc=StripAddress(theShutdownProc);
theRestartProc=StripAddress(theRestartProc);
Mymemcpy(theShutdownProc, *theProcHandle, theSize);
Mymemcpy(theRestartProc, *theProcHandle, theSize);
ReleaseResource(theProcHandle);
theProcHandle=0L;
}
else
{
resultCode=GetGestaltProcPtrs(&dummy, &theShutdownProc, &theRestartProc);
if (resultCode!=allsWell)
return resultCode;
}
*(ProcPtr*)((long)theShutdownProc+6)=
*(ProcPtr*)((long)theRestartProc+6)=theFade; /* store address of fade */
*(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0; /* store on shutdown flag */
*(short*)((long)theRestartProc+10)=onRestart ? 1 : 0; /* store on restart flag */
if (installProcs)
{
ShutDwnInstall(theShutdownProc, isEarly ? sdOnPowerOff+sdOnDrivers : sdOnPowerOff);
ShutDwnInstall(theRestartProc, isEarly ? sdOnRestart+sdOnDrivers: sdOnRestart);
}
gModuleIndex=index;
resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
return resultCode;
}
enum ErrorTypes RemoveTheFade(void)
{
ProcPtr theFade, theShutdownProc, theRestartProc;
enum ErrorTypes resultCode;
resultCode=GetGestaltProcPtrs(&theFade, &theShutdownProc, &theRestartProc);
if (resultCode!=allsWell)
return resultCode;
resultCode=SetGestaltProcPtrs(0L, theShutdownProc, theRestartProc);
if (resultCode!=allsWell)
return resultCode;
if (theFade!=0L)
DisposePtr(theFade);
if (theShutdownProc!=0L)
*(short*)((long)theShutdownProc+10)=0;
if (theRestartProc!=0L)
*(short*)((long)theRestartProc+10)=0;
return allsWell;
}
OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
{
unsigned long oldTopMapHndl;
OSErr theResError;
*oldRefNum=CurResFile();
oldTopMapHndl=(unsigned long)TopMapHndl;
*newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
theResError=ResError();
*alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
return theResError;
}
void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
{
if (!alreadyOpen)
{
CloseResFile(newRefNum);
}
UseResFile(oldRefNum);
}